<template>
  <DataList 
  :excelHeader="excelHeader" 
  :cate="cate" 
  :cols="cols" 
  ref="datalist"
  
  :name="name"
  skipExcelNumber="3" 

  />
</template>
<script>
import DataList from "@/components/DataList";
import { getUser } from "@/utils/auth";
import linq from "linq"
import { addXsst, deleteXsst, updateXsst, getXsst,bulkAddXsst } from "@/api/学生社团.js";
import {findComponent} from "@/utils/ui";

export default {
  components: {
    DataList
  },
  mounted() {
    console.log(this.$root)
  },
  data() {
    return {
      cate: "A10-3学生社团表",
      name:'10.3 学生社团',
      excelHeader:{B3:"社团代码",C3:"社团名称（全称）",D3:"社团类别",E3:"登记情况",G3:"注册单位",H3:"现有成员（人）",
      I3:"负责人情况",K3:"活动经费（元）",L3:"是否设有学分（学时）",M3:"是否有获奖项目",N3:"学校指导部门",O3:"指导教师名单",
      E4:"登记日期（年月）",F4:"批准单位（部门）",I4:"姓名",J4:"所在年级",},
      cols: [
        { label: "社团代码", prop: "STDM" },
        { label: "社团名称（全称）", prop: "STMC" },
        { label: "社团类别", prop: "STLB" },
        { label: "登记情况", prop: "登记情况" ,
            data:[
            {label:'登记日期（年月）',prop:'DJQK_DJRQ'},
            {label: "批准单位（部门）", prop: "DJQK_PZDW" },
        ]},
        
        { label: "注册单位", prop: "ZCDW" },
        { label: "现有成员（人）", prop: "XYQY" },
        { label: "负责人情况",
          data: [
            { label: "姓名", prop: "XM" },
            { label: "所在年级", prop: "SZNJ" },
          ]
        },
        { label: "活动经费（元）", prop: "HDJF" },
        { label: "是否设有学分（学时）", prop: "XZ_SFYXF" },
        { label: "是否有获奖项目", prop: "XZ_SFHJXM" },
        { label: "学校指导部门", prop: "XXZDBM" },
        { label: "指导教师名单", prop: "ZDJSH" },
        
      ]
    };
  },
  methods: {
      getSummaries(list){
      //
        const sums = [];
         this.cols.forEach((column, index) => {
            sums[index]="";
        });
        sums[0]= "合计";
        sums[6]= "社团数:"+linq.from(list).distinct("$.STMC").count();
        sums[8]= "类别数:"+linq.from(list).distinct("$.STLB").count();
        sums[12]= ""+linq.from(list).sum("$.XYQY");
        sums[15]= ""+linq.from(list).sum("$.HDJF").toFixed(2);
        sums[16]= "有学分"+linq.from(list).where("$.XZ_SFYXF=='是'").count();
        sums[17]= "有获奖"+linq.from(list).where("$.XZ_SFHJXM=='是'").count();
        sums[20]= "通过:"+linq.from(list).where("$.审核状态=='通过'").count();
        sums[20] += ",不通过:"+linq.from(list).where("$.审核状态=='不通过'").count();
        sums[20] += ",待审核:"+linq.from(list).where("$.审核状态=='待审核'").count();
        
       return sums;
    //   //
    },

    // //导入检查方法
     check(array,list) {
     return new Promise(async (resolve,reject)=>{
         try{
          var user = getUser();
          var 学生社团 = await getXsst(user.XXID,user.CJPC)
          
          //本身重复性检查
          
          var duplicates =  linq.from(array).groupBy("$.STDM").where("$.count()>1").toArray();
          duplicates.forEach(e=>{
            linq.from(array).where(t=>t.STDM==e.key()).forEach(t=>t.error="录入数据存在相同的社团代码");
          })
          //是否已经存在数据
        
          var err =  linq.from(array).select("$.STDM").intersect(linq.from(list).select("$.STDM").toArray()).toArray();
          linq.from(array).where(e=>err.indexOf(e.STDM) !==-1).forEach(e=>e.error=e.error?e.error+";":''+"数据库中已经存在对应的社团代码");
          
          //是否已经存在相同
          
          err =  linq.from(array).select("$.STDM").except(linq.from().select("$.STDM").toArray()).toArray();
    
        
            resolve();
          }catch(e){
            reject(e);
          }
     }) 
    },

    async fetchData() {
      var user = getUser();
 	    var datas = await getXsst(user.XXID,user.CJPC);
      return datas;
    },
    add_check(model,list){
      //  
         //是否已经存在数据
          var first =  linq.from(list).firstOrDefault(e=>e.STDM ==  model.STDM);
          if(first)
          return  "已经存在对应社团代码的社团"
      //
    },
     onadd(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))

      return new Promise(async (resolve,reject)=>{  
           
       try{     
          
          console.log(JSON.stringify(temp));
          var model = await addXsst(temp);
          this.add_check()
          resolve(model);
           }catch(e){
        reject(e);
      } 
      });
      
    },
    async  bulkAdd(array){
       await  bulkAddXsst({Xsst:array});
    },
    onupdate(model) {
      //
      var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
           
          console.log(JSON.stringify(temp));
          var model = await updateXsst(temp);
          resolve(model);
      })
      //
    },
    ondelete(model) {
       var temp = JSON.parse(JSON.stringify(model))
      return new Promise(async (resolve,reject)=>{
        
         await deleteXsst(temp);
          resolve();
      })
    }
  }, 
};
</script>
<style>
</style>